package com.five.second_shop.controller.User;

import com.five.second_shop.entity.Commodity;
import com.five.second_shop.entity.Order;
import com.five.second_shop.service.AddressService;
import com.five.second_shop.service.CommodityService;
import com.five.second_shop.service.OrderService;
import com.five.second_shop.util.KeyUtil;
import com.five.second_shop.util.ObjectToMap;
import com.five.second_shop.util.StatusCode;
import com.five.second_shop.vo.LayuiPageVo;
import com.five.second_shop.vo.ResultVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private CommodityService commodityService;

    //创建订单
    @ResponseBody
    @RequestMapping("/order/inserorderlist")
    public ResultVo insertOrder(@RequestBody Order order, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        order.setUserid(KeyUtil.genUniqueKey()).setUserid(userid);
        Integer i = orderService.insertOrder(order);
        if (i == 1) {
            return new ResultVo(true, StatusCode.OK, "订单创建成功");
        }
        return new ResultVo(false, StatusCode.ERROR, "订单创建失败，请重新");
    }

    //删除订单
    @ResponseBody
    @RequestMapping("/order/delect/{id}")
    public ResultVo delectOrder(@PathVariable("id") String id, HttpSession session) {
        String username = (String) session.getAttribute("username");
        Order order = orderService.queryOrderById(id);
        if (StringUtils.isEmpty(order)) {
            return new ResultVo(false, StatusCode.FINDERROR, "找不到要删除的订单");
        }
        return new ResultVo(true, StatusCode.OK, "删除成功");
    }

    /**
     * 获取用户的订单信息
     * @param session
     * @param limit 由框架在url上自动补全
     * @param page 由框架在url上自动补全
     * @return
     * @throws Exception
     */
     @RequestMapping("/order/orderList")
     @ResponseBody
    public LayuiPageVo  orderList(HttpSession session,int limit, int page) throws Exception {
         String userid = session.getAttribute("userid").toString();
         Integer dataNum = orderService.queryOrderCount(userid);
         System.out.println(dataNum);
         List<Order> orders = orderService.queryAllOrder((page - 1) * limit, limit, new Order().setUserid(userid));
         System.out.println(orders);
         return new LayuiPageVo("",0,dataNum,orders);
     }

    /**
     * 通过id搜索获取
     * @param orderid
     * @param limit
     * @param page
     * @return
     */
     @RequestMapping("/order/getOne/{orderid}")
     @ResponseBody
    public LayuiPageVo getOne(@PathVariable("orderid") String orderid,int limit, int page){
         List<Order> orders = orderService.queryAllOrder((page - 1) * limit, limit, new Order().setOrderid(orderid));
         return new LayuiPageVo("",0,1,orders);
     }

}
